export const state = () => {
  return {
    loginModalVisible: false,
    userInfo: '',
    notifyCount: {},
    previewImgSrc: null,
    siteConfig: {},
    imsocket: null,
    imMessage: null
  }
}

export const mutations = {
  UPDATE_LOGIN_VISIBLE (state, loginModalVisible) {
    state.loginModalVisible = loginModalVisible
  },

  UPDATE_USER_INFO (state, userInfo) {
    state.userInfo = userInfo
  },

  UPDATE_NOTIFY_COUNT (state, notifyCount) {
    state.notifyCount = notifyCount
  },

  UPDATE_PREVIEW_IMGSRC (state, previewImgSrc) {
    state.previewImgSrc = previewImgSrc
  },

  UPDATE_SITE_CONFIG (state, siteConfig) {
    state.siteConfig = siteConfig
  },

  UPDATE_IM_SOCKET (state, imsocket) {
    state.imsocket = imsocket
  },

  UPDATE_IM_MESSAGE (state, imMessage) {
    state.imMessage = imMessage
  }
}

export const actions = {
  async nuxtServerInit ({ commit }, { app }) {
    try {
      const { data: configInfo } = await app.$axios.get(`/gimishare/api/v1/config`)
      commit('UPDATE_SITE_CONFIG', configInfo)
      const { data: userInfo } = await app.$axios.get(
        '/gimishare/api/v1/users/sessionuserinfo?noredirect'
      )
      commit('UPDATE_USER_INFO', userInfo)
    } catch (error) {
      console.log(error)
    }
  }
}
